!
! Copyright (C) 2000-2013 A. Marini and the YAMBO team 
!              https://code.google.com/p/rocinante.org
! 
! This file is distributed under the terms of the GNU 
! General Public License. You can redistribute it and/or 
! modify it under the terms of the GNU General Public 
! License as published by the Free Software Foundation; 
! either version 2, or (at your option) any later version.
!
! This program is distributed in the hope that it will 
! be useful, but WITHOUT ANY WARRANTY; without even the 
! implied warranty of MERCHANTABILITY or FITNESS FOR A 
! PARTICULAR PURPOSE.  See the GNU General Public License 
! for more details.
!
! You should have received a copy of the GNU General Public 
! License along with this program; if not, write to the Free 
! Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, 
! MA 02111-1307, USA or visit http://www.gnu.org/copyleft/gpl.txt.
!
integer function ioE_RIM(E,ID)
 !
 use pars,           ONLY:SP,IP
 use electrons,      ONLY:levels,BZ_RIM_nkpt,BZ_RIM_table,BZ_RIM_max_filling,&
&                         BZ_RIM_nbands,BZ_RIM_tot_nkpts,n_sp_pol,BZ_RIM_ipol_weight
 use R_lattice,      ONLY:nXkbz
 use memory_m,       ONLY:mem_est
 use IO_m,           ONLY:io_connect,io_disconnect,io_elemental,read_is_on,&
&                         io_bulk,io_header,ver_is_gt_or_eq
 implicit none
 !
 type(levels) :: E
 integer      :: ID 
 !
 ioE_RIM=io_connect(desc='E_RIM',type=2,ID=ID)
 if (ioE_RIM/=0) goto 1
 !
 ioE_RIM=io_header(ID)
 call io_elemental(ID,VAR="PARS",VAR_SZ=3,MENU=1)
 call io_elemental(ID,I0=BZ_RIM_nbands,    VAR=' Bands                           :')
 call io_elemental(ID,I0=BZ_RIM_tot_nkpts, VAR=' BZ RIM k-points                 :')
 call io_elemental(ID,I0=BZ_RIM_max_filling,VAR=' BZ Blocks max filling           :')
 call io_elemental(ID,VAR="",VAR_SZ=0,MENU=1)
 !
 if (read_is_on(ID)) then
   allocate(E%E_RIM(BZ_RIM_nbands,BZ_RIM_tot_nkpts,n_sp_pol))
   allocate(E%f_RIM(BZ_RIM_nbands,BZ_RIM_tot_nkpts,n_sp_pol))
   allocate(BZ_RIM_nkpt(nXkbz),BZ_RIM_table(nXkbz,BZ_RIM_max_filling))
   allocate(BZ_RIM_ipol_weight(BZ_RIM_max_filling))
   call mem_est("E_RIM f_RIM BZ_RIM_nkpt BZ_RIM_table",&
&               (/size(E%E_RIM),size(E%f_RIM),size(BZ_RIM_nkpt),&
&                 size(BZ_RIM_table)/),(/SP,SP,IP,IP/))
 endif
 !
 ! Eigenvalues
 !
 call io_bulk(ID, VAR="RIM_EIGENVALUES", VAR_SZ=shape(E%E_RIM) ) 
 call io_bulk(ID, R3=E%E_RIM )
 !
 ! RIM Block filling
 !
 call io_bulk(ID, VAR="RIM_BLOCK_FILLING", VAR_SZ=shape(BZ_RIM_nkpt) ) 
 call io_bulk(ID, I1=BZ_RIM_nkpt )
 !
 ! RIM Block table
 !
 call io_bulk(ID, VAR="RIM_BLOCK_TABLE", VAR_SZ=shape(BZ_RIM_table) ) 
 call io_bulk(ID, I2=BZ_RIM_table )
 !
 ! RIM double grid interpolation weights of each element in table
 !
 if (ver_is_gt_or_eq(ID,revision=1300)) then
   call io_bulk(ID, VAR="RIM_INTERPOL_WEIGHT", VAR_SZ=shape(BZ_RIM_ipol_weight) ) 
   call io_bulk(ID, R1=BZ_RIM_ipol_weight)
 else
   BZ_RIM_ipol_weight=1._SP
 endif
 !
1 call io_disconnect(ID=ID)
 !
end function
